<template>
    <div>
        <Row :gutter="20">
            <i-col :xs="12" :md="8" :lg="4" v-for="(infor, i) in inforCardData" :key="`infor-${i}`" style="height: 120px;padding-bottom: 10px;">
                <infor-card shadow :color="infor.color" :icon="infor.icon" :icon-size="36">
                <count-to :end="infor.count" count-class="count-style"/>
                <p>{{ infor.title }}</p>
                </infor-card>
            </i-col>
        </Row>
        <Row :gutter="20" v-if="showstatistics">
          <i-col :md="24" :lg="24" >
            <Card shadow>
              <chart-bar style="height:500px;width:100%" v-model="statisticslist" text="数据统计"/>
            </Card>
          </i-col>
          <!--<i-col :md="24" :lg="24" >
             <Table :columns="column" :data="list"></Table>
          </i-col>-->
        </Row>
    </div>
</template>
<script>
import InforCard from '_c/info-card'
import CountTo from '_c/count-to'
import { ChartBar } from '_c/charts'
import { getcountlist, getstatistics } from '@/api/home'
import { isNull } from '@/libs/tools'
export default {
  name: 'home',
  components: {
    InforCard,
    CountTo,
    ChartBar
  },
  data () {
    var self = this
    return {
      column: [
        {
          title: '类型',
          key: 'kind',
          width: 150,
          render: function (h, params) {
            const kind = self.$t('kind_' + params.row.kind)
            const html = kind
            var div = h('div', {
              domProps: {
                innerHTML: html
              }
            })
            return div
          }
        },
        {
          title: '状态',
          key: 'status',
          render: function (h, params) {
            let strarr = []
            if (params.row.ispass >= 0 || !isNull(params.row.ispass)) {
              const ispass = self.$t('ispass_' + params.row.ispass)
              strarr.push('<p>审核通过：' + ispass + '</p>')
            }
            if (params.row.isactive >= 0 || !isNull(params.row.isactive)) {
              const isactive = self.$t('isactive_' + params.row.isactive)
              console.log(isactive)
              strarr.push('<p>可用状态：' + isactive + '</p>')
            }
            if (params.row.ispay >= 0 || !isNull(params.row.ispay)) {
              const ispay = self.$t('ispay_' + params.row.ispay)
              strarr.push('<p>支付状态：' + ispay + '</p>')
            }
            if (params.row.isright >= 0 || !isNull(params.row.isright)) {
              const isright = self.$t('isright_' + params.row.isright)
              strarr.push('<p>权限状态：' + isright + '</p>')
            }

            const html = strarr.join('')
            var div = h('div', {
              domProps: {
                innerHTML: html
              }
            })
            return div
          }
        },
        {
          title: '总数',
          key: 'count',
          width: 150
        }
      ],
      list: [],
      showstatistics: false,
      statisticslist: {},
      inforCardData: []
    }
  },
  methods: {
    getcountlist () {
      getcountlist().then(res => {
        const data = res.data
        if (data.code === 200) {
          for (let key in data.data) {
            switch (key) {
              case 'userinfo':
                this.inforCardData.push({ title: '会员', icon: 'md-person-add', count: data.data[key], color: '#2d8cf0' })
                break
              case 'company':
                this.inforCardData.push({ title: '企业', icon: 'ios-nutrition', count: data.data[key], color: '#536E80' })
                break
              case 'invent':
                this.inforCardData.push({ title: '招工', icon: 'ios-hammer', count: data.data[key], color: '#19be6b' })
                break
              case 'jobhunting':
                this.inforCardData.push({ title: '找活', icon: 'ios-information-circle', count: data.data[key], color: '#ff9900' })
                break
              case 'payorder':
                this.inforCardData.push({ title: '订单', icon: 'ios-cash', count: data.data[key], color: '#3399ff' })
                break
            }
          }
        } else {
          this.$Message.error(data.msg)
        }
      })
    },
    getstatisticslist () {
      getstatistics().then(res => {
        const data = res.data
        var sumunable = 0
        if (data.code === 200) {
          for (let key in data.data) {
            switch (key) {
              case 'userinfo':
                sumunable = 0
                for (var i = 0; i < data.data[key].length; i++) {
                  if (data.data[key][i].isactive == 1) {
                    if (data.data[key][i].isright == 1) {
                      this.statisticslist.会员可发布 = data.data[key][i].count
                    } else if (data.data[key][i].isright == 0) {
                      this.statisticslist.会员不可发布 = data.data[key][i].count
                    }
                  } else {
                    sumunable += data.data[key][i].count
                  }
                }
                this.statisticslist.会员无效 = sumunable
                break
              case 'invent':
                sumunable = 0
                for (var j = 0; j < data.data[key].length; j++) {
                  if (data.data[key][j].isactive == 1) {
                    if (data.data[key][j].ispass == 1) {
                      this.statisticslist.招工审核通过 = data.data[key][j].count
                    } else if (data.data[key][j].ispass == 2) {
                      this.statisticslist.招工审核失败 = data.data[key][j].count
                    } else if (data.data[key][j].ispass == 0) {
                      this.statisticslist.招工未审核 = data.data[key][j].count
                    }
                  } else {
                    sumunable += data.data[key][j].count
                  }
                }
                this.statisticslist.招工关闭 = sumunable
                break
              case 'jobhunting':
                sumunable = 0
                for (var k = 0; k < data.data[key].length; k++) {
                  if (data.data[key][k].isactive == 1) {
                    if (data.data[key][k].ispass == 1) {
                      this.statisticslist.找活审核通过 = data.data[key][k].count
                    } else if (data.data[key][k].ispass == 2) {
                      this.statisticslist.找活审核失败 = data.data[key][k].count
                    } else if (data.data[key][k].ispass == 0) {
                      this.statisticslist.找活未审核 = data.data[key][k].count
                    }
                  } else {
                    sumunable += data.data[key][k].count
                  }
                }
                this.statisticslist.找活关闭 = sumunable
                break
              case 'company':
                sumunable = 0
                for (var k = 0; k < data.data[key].length; k++) {
                  if (data.data[key][k].isactive == 1) {
                    if (data.data[key][k].ispass == 1) {
                      this.statisticslist.企业审核通过 = data.data[key][k].count
                    } else if (data.data[key][k].ispass == 2) {
                      this.statisticslist.企业审核失败 = data.data[key][k].count
                    } else if (data.data[key][k].ispass == 0) {
                      this.statisticslist.企业未审核 = data.data[key][k].count
                    }
                  } else {
                    sumunable += data.data[key][k].count
                  }
                }
                this.statisticslist.企业不可用 = sumunable
                break
              case 'payorder':
                for (var k = 0; k < data.data[key].length; k++) {
                  if (data.data[key][k].ispay === 1) {
                    this.statisticslist.订单已支付 = data.data[key][k].count
                  } else {
                    this.statisticslist.订单未支付 = data.data[key][k].count
                  }
                }
                break
            }
          }
          this.showstatistics = true
          // console.log(this.statisticslist)
        } else {
          this.$Message.error(data.msg)
        }
      })
    }
    /* getstatisticslist () {
      getstatistics().then(res => {
        const data = res.data
        if (data.code === 200) {
          this.list = data.data
          this.showstatistics = true
        } else {
          this.$Message.error(data.msg)
        }
      })
    } */
  },
  mounted () {
    this.getcountlist()
    this.getstatisticslist()
  }
}
</script>
